package cn.lbd;

import org.junit.Test;

import java.nio.ByteBuffer;

public class BufferDemo {
    private ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    @Test
    public void test1() {

        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println();

        String str = "abcde";
        byte[] bytes = str.getBytes();
        byteBuffer.put(bytes);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println();

        byteBuffer.flip();
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println();

        byte[] bytes1 = new byte[byteBuffer.remaining()];
        this.byteBuffer.get(bytes1, 0, 2);
        System.out.println(new String(bytes1));
        System.out.println(this.byteBuffer.position());
        System.out.println(this.byteBuffer.limit());
        System.out.println(this.byteBuffer.capacity());
        System.out.println();

        this.byteBuffer.mark();

        this.byteBuffer.get(bytes1,2,3);
        System.out.println(new String(bytes1));
        System.out.println(this.byteBuffer.position());
        System.out.println(this.byteBuffer.limit());
        System.out.println(this.byteBuffer.capacity());
        System.out.println();

        this.byteBuffer.reset();
        System.out.println(this.byteBuffer.position());
        System.out.println(this.byteBuffer.limit());
        System.out.println(this.byteBuffer.capacity());
        System.out.println();


        this.byteBuffer.clear();
        System.out.println(this.byteBuffer.position());
        System.out.println(this.byteBuffer.limit());
        System.out.println(this.byteBuffer.capacity());
        System.out.println();
    }
}
